<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\DictDataRequest;
use App\Models\SysDictData;
use Illuminate\Http\Request;

class SysDictDataController extends ApiBaseController
{
    public function index(Request $request)
    {
        // 
        // ->where('status', 1)
        $paginator = SysDictData::filter($request->all())->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function show(Request $request, SysDictData $model)
    {
        $id = $request->get('id');
        $data = $model->find($id);
        return success(['data' => $data->toArray()]);
    }

    public function create(DictDataRequest $request)
    {
        $model = SysDictData::create($request->all());
        return success([], '添加成功');
    }

    public function update(DictDataRequest $request, SysDictData $model)
    {
        $id = $request->post('id');
        $sysDictType = $model->find($id);

        if ($sysDictType) {
            $sysDictType->update($request->all());
        } else {
            return error('数据不存在');
        }

        return success([], '更新成功');
    }

    public function destroy($ids, Request $request, SysDictData $model)
    {
        $id_arr = explode(',', $ids);
        $model->destroy($id_arr);
    }
}
